﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;

namespace PBO.Util
{
    public class ListSorter : IComparer<ListViewItem>, IComparer
    {
        private SortOrder order;

        public int Column { get; private set; }

        public ListSorter()
            : this(0) { }

        public ListSorter(int column)
        {
            this.Column = column;
            order = SortOrder.Ascending;
        }

        public void SetColumn(int column)
        {
            if (this.Column == column)
            {
                if (order == SortOrder.Ascending)
                {
                    order = SortOrder.Descending;
                }
                else
                {
                    order = SortOrder.Ascending;
                }
            }
            else
            {
                this.Column = column;
                order = SortOrder.Ascending;
            }
        }

        public int Compare(ListViewItem x, ListViewItem y)
        {
            string text1 = x.SubItems[this.Column].Text;
            string text2 = y.SubItems[this.Column].Text;
            int result = 0;
            double number1 = 0;
            double number2 = 0;
            if (double.TryParse(text1, out number1) && double.TryParse(text2, out number2))
            {
                result = number1.CompareTo(number2);
            }
            else
            {
                result = string.Compare(text1, text2);
            }
            if (order == SortOrder.Descending)
                result = -result;
            return result;
        }

        public int Compare(object x, object y)
        {
            return this.Compare((ListViewItem)x, (ListViewItem)y);
        }
    }

    public class RowComparer : IComparer<DataGridViewRow>, IComparer
    {
        public int Column { get; private set; }


        private static int sortOrderModifier = 1;

        public RowComparer(SortOrder sortOrder)
        {
            if (sortOrder == SortOrder.Descending)
            {
                sortOrderModifier = -1;
            }
            else if (sortOrder == SortOrder.Ascending)
            {
                sortOrderModifier = 1;
            }
        }

        public int Compare(object x, object y)
        {
            DataGridViewRow row1 = (DataGridViewRow)x;
            DataGridViewRow row2 = (DataGridViewRow)y;
            int result = 0;
            var cell1 = row1.Cells[this.Column];
            var cell2 = row2.Cells[this.Column];

            if (cell1.ValueType == typeof(Image) || cell2.ValueType == typeof(Image))
            {
                result = string.Compare(cell1.Tag.ToString(), cell2.Tag.ToString());
            }
            else
            {
                result = Compare(cell1.Value.ToString(), cell2.Value.ToString());
            }

            return result * sortOrderModifier;
        }

        public int Compare(DataGridViewRow x, DataGridViewRow y)
        {
            // Try to sort based on the Last Name column. 
            int CompareResult = System.String.Compare(
                x.Cells[1].Value.ToString(),
                y.Cells[1].Value.ToString());

            // If the Last Names are equal, sort based on the First Name. 
            if (CompareResult == 0)
            {
                CompareResult = System.String.Compare(
                    x.Cells[0].Value.ToString(),
                    y.Cells[0].Value.ToString());
            }
            return CompareResult * sortOrderModifier;
        }

        private static int Compare(string strA, string strB)
        {
            int result = 0;
            double number1 = 0;
            double number2 = 0;
            if (double.TryParse(strA, out number1) && double.TryParse(strB, out number2))
            {
                result = number1.CompareTo(number2);
            }
            else
            {
                result = string.Compare(strA, strB);
            }
            return result;
        }
    }
}
